package com.chuanyekeji.aokai.Controller;

import com.chuanyekeji.aokai.Entity.CustomerMessage;
import com.chuanyekeji.aokai.Mapper.CustomerMessageMapper;
import com.chuanyekeji.aokai.Util.DateUtil;
import com.chuanyekeji.aokai.Util.ReturnData;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
public class CustomerMessageController {

    @Autowired
    CustomerMessageMapper customerMessageMapper;

    /**
     * 添加用户留言
     * @param customerMessage
     * @return
     */
    @RequestMapping("addCustomerMessage")
    String addCustomerMessage(CustomerMessage customerMessage) {
        customerMessage.setCreatetime(DateUtil.dateTimeToString(new Date()));
        customerMessageMapper.insertSelective(customerMessage);
        return ReturnData.success("留言成功");
    }

    /**
     * 获取用户留言列表
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("getCustomerMessageList")
    String getCustomerMessageList(@RequestParam(required=false,defaultValue="1") int page, @RequestParam(required=false,defaultValue="10") int rows) {
        int lastRow = rows * page;
        int indexRow = lastRow - rows;
        List<CustomerMessage> customerMessageList = customerMessageMapper.getCustomerMessageList(indexRow,lastRow);
        Map<String,Object> data = new HashMap<>();
        data.put("total",customerMessageMapper.getCustomerMessageCount());
        data.put("rows",customerMessageList);
        //return ReturnData.success("success",data);
        return new Gson().toJson(data);
    }

    /**
     * 删除用户留言
     * @param id
     * @return
     */
    @RequestMapping("delCustomerMessage")
    String delCustomerMessage(int id) {
        customerMessageMapper.deleteByPrimaryKey(id);
        return "删除成功";
    }
}
